<div id="app">
    <el-card class="box-card">
<!--        <el-form :inline="true">-->
<!--            <el-form-item label="入库单号：" label-width="100px">-->
<!--                <div style="display: flex;width: 300px;">-->
<!--                    <el-input v-model="data.receipt_no" placeholder="请输入入库单号"></el-input>-->
<!--                    <el-button type="primary" icon="el-icon-search" @click="get_list" ></el-button>-->
<!--                </div>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="入库时间：" label-width="100px">-->
<!--                <div style="display: flex;width: 300px;">-->
<!--                    <el-date-picker-->
<!--                            v-model="value1"-->
<!--                            type="datetimerange"-->
<!--                            start-placeholder="开始日期"-->
<!--                            end-placeholder="结束日期"-->
<!--                            :default-time="['00:00:00']">-->
<!--                    </el-date-picker>-->
<!--                    <el-button type="primary" icon="el-icon-search" @click="get_list_time" ></el-button>-->
<!--                </div>-->
<!--            </el-form-item>-->
<!--        </el-form>-->
<!--                <el-button type="primary" size="small" @click="onSubmit">新建入库单</el-button>-->
<!--        <div style="height: 30px;"></div>-->
<!--        <el-table v-if="dataArr.length>0"-->
<!--                  :data="dataArr"-->
<!--                  border-->
<!--                  style="width: 100%">-->
<!--            <el-table-column-->
<!--                    prop="id"-->
<!--                    label="ID"-->
<!--                    width="50">-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--                    prop="receipt_no"-->
<!--                    label="入库单号"-->
<!--                    width="180">-->
<!--            </el-table-column>-->
<!--&lt;!&ndash;            <el-table-column&ndash;&gt;-->
<!--&lt;!&ndash;                    prop="receipt_name"&ndash;&gt;-->
<!--&lt;!&ndash;                    label="入库单名称"&ndash;&gt;-->
<!--&lt;!&ndash;            >&ndash;&gt;-->
<!--&lt;!&ndash;            </el-table-column>&ndash;&gt;-->

<!--            <el-table-column-->
<!--                             prop="status"-->
<!--                             label="入库状态"-->
<!--            >-->
<!--                <template slot-scope="{row}">-->
<!--                    <span v-if="row.status==1">入库中</span>-->
<!--                    <span v-else-if="row.status==2">入库完成</span>-->
<!--                </template>-->
<!--            </el-table-column>-->
<!--            <el-table-column-->
<!--                             prop="receipt_type"-->
<!--                             label="入库类型"-->
<!--            >-->
<!--                <template slot-scope="{row}">-->
<!--                    <span v-if="row.receipt_type==1">采购入库</span>-->
<!--                    <span v-else-if="row.receipt_type==1">采购入库</span>-->
<!--                    <span v-else-if="row.receipt_type==2">作废入库</span>-->
<!--                    <span v-else-if="row.receipt_type==3">销售出库</span>-->
<!--                    <span v-else-if="row.receipt_type==4">盘盈入库</span>-->
<!--                    <span v-else-if="row.receipt_type==5">盘亏出库</span>-->
<!--&lt;!&ndash;                    <span v-else-if="row.receipt_type==2">作废入库</span>&ndash;&gt;-->
<!--                </template>-->
<!--            </el-table-column>-->

<!--            <el-table-column-->
<!--                    prop="warehousing_time"-->
<!--                    label="入库时间"-->
<!--            >-->
<!--            </el-table-column>-->

<!--            <el-table-column-->
<!--                    prop="reamark"-->
<!--                    label="备注"-->
<!--            >-->
<!--            </el-table-column>-->

<!--            <el-table-column label="操作" width="200">-->
<!--                <template slot-scope="{row}">-->
<!--                    <el-button type="text" size="mini" @click="detail_show(row)">详情</el-button>-->
<!--                    <el-button v-if="row.status==1" type="text" size="mini" @click="edit_show(row)">编辑</el-button>-->
<!--                    <el-button type="text" size="mini" @click="notes_show(row)">备注</el-button>-->
<!--                </template>-->
<!--            </el-table-column>-->
<!--        </el-table>-->
<!--        <div class="block">-->
<!--            <el-pagination-->
<!--                    @size-change="handleSizeChange"-->
<!--                    @current-change="handleCurrentChange"-->
<!--                    :current-page="currentPage"-->
<!--                    :page-sizes="[10, 20, 30, 40]"-->
<!--                    :page-size="10"-->
<!--                    layout="total, sizes, prev, pager, next, jumper"-->
<!--                    :total="total">-->
<!--            </el-pagination>-->
<!--        </div>-->

        <div >
            <div style="width: 600px;">
                <el-input v-model="order_code_form" :autofocus="true"  placeholder="请扫描 / 手动输入商品条码" @keyup.enter.native="keydown"></el-input>

            </div>
            <div><span style="line-height: 3;font-weight: bold;">入库商品</span>
            </div>
            <el-table :data="add_form.goodsw_item" border style="width: 700px;">
<!--                <el-table-column label="序号" prop="gid" width="110"></el-table-column>-->
                <el-table-column label="商品" prop="good_name" width="200"></el-table-column>
<!--                <el-table-column label="商品分类" prop="good_type_name" width="110"></el-table-column>-->
                <el-table-column label="入库数量" prop="num" >
                    <template slot-scope="{row}">
                        <el-input type="number" min="1" v-model="row.num"></el-input>
                    </template>
                </el-table-column>
<!--                <el-table-column label="操作"  >-->
<!--                    <template slot-scope="{row}">-->
<!--                        <el-button type="text" @click="del_good(row.gid)">删除</el-button>-->
<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
            <div style="display: flex; align-items: center;">
                <div>
                    <el-button type="primary" @click="reload">重置</el-button>
                    <!--                        <el-button type="primary" @click="onSave">保存</el-button>-->
                    <el-button type="primary" @click="success_">完成入库</el-button>
                    <!--                        <el-button type="primary" @click="exit">退出</el-button>-->
                </div>
            </div>
        </div>

    </el-card>
    <el-dialog
            :visible.sync="dialogVisible"
            width="70%"
    >

        <div v-if="false">
            <div>

                <div style="width: 500px;margin: 20px ;"  class="search_box">
                    <el-input v-model="order_code_form" :autofocus="true" placeholder="请扫描 / 手动输入商品条码" @keyup.enter.native="keydown_deit"></el-input>
                </div>

            </div>
            <el-table :data="edit_detail.goodsw_item"
                      border>
                <el-table-column label="商品" prop="good_name" width="200"></el-table-column>
                <el-table-column label="入库数量" prop="num" width="110">
                    <template slot-scope="{row}">
                        <el-input type="number" min="1" v-model="row.num"></el-input>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex; align-items: center;margin-top: 20px;">
                <div>
                    <el-button type="primary" @click="reload">重置</el-button>
                    <el-button type="primary" @click="edit_save(2)">完成入库</el-button>
                </div>
            </div>
        </div>

    </el-dialog>
    <el-dialog
            title="变更提示"
            :visible.sync="dialogVisible_change"
            width="30%"
    >
        <el-select v-model="change_info_val">
            <el-option label="收货信息"
                       :value="1"></el-option>
            <el-option label="订单商品"
                       :value="2"></el-option>
        </el-select>
        <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible_change = false">取 消</el-button>
    <el-button type="primary" @click="success">确 定</el-button>
  </span>
    </el-dialog>
</div>
<script !src="">
    new Vue({
        el: '#app',
        data: function() {
            return {
                form:{},
                dataArr:[],
                labelPosition:'left',
                bh:'',
                value1:'',
                value2:'',
                data:{
                    page:1,
                    limit:10,
                    receipt_no:'',
                    status_time:'',
                    end_time:''
                },
                total:0,
                currentPage:1,
                dialogVisible: false,
                active_int:1,
                detail_obj:{},
                dialogVisible_change:false,
                change_info_val:1,
                change_info_id:'',
                pickerOptions:'',
                edit_detail:{},
                add_form:{
                    tracking_numberkd:'',
                    receipt_name:'',
                    status:0,
                    receipt_no:'',
                    receipt_username:'',
                    receipt_id:'',
                    warehousing_time:'',
                    receipt_type:1,
                    goodsw_item:[]
                },
                order_code_form:''
            }
        },
        created(){
            this.get_list()
        },
        methods:{
            reload(){
                this.add_form={
                    receipt_name:'',
                    status:0,
                    receipt_no:'',
                    receipt_username:'',
                    receipt_id:'',
                    warehousing_time:'',
                    goodsw_item:[]
                }
            },
            get_list_time(){
                var timdedetail = this.value1[0]
                this.data.status_time = this.getData(timdedetail)

                var end_time = this.value1[1]
                this.data.end_time = this.getData(end_time)
                this.get_list()
            },
            getData(timdedetail){
                var year=timdedetail.getFullYear();
                var month=(timdedetail.getMonth()+1)>=10?timdedetail.getMonth()+1:'0'+(timdedetail.getMonth()+1);
                var date=timdedetail.getDate()>=10?timdedetail.getDate():'0'+timdedetail.getDate();
                var hours=timdedetail.getHours()>=10?timdedetail.getHours():'0'+timdedetail.getHours();
                var minutes=timdedetail.getMinutes()>=10?timdedetail.getMinutes():'0'+timdedetail.getMinutes();
                var seconds=timdedetail.getSeconds()>=10?timdedetail.getSeconds():'0'+timdedetail.getSeconds();;
                var time=year+"-"+month+"-"+date+" "+hours+":"+minutes+":"+seconds;
                return time
            },
            del_good(id){
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var index = this.add_form.goodsw_item.findIndex(d=>d.gid==id)
                    this.add_form.goodsw_item.splice(index,1)
                })
            },
            del_good_edit(id){
                this.$confirm('确认删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var index = this.edit_detail.goodsw_item.findIndex(d=>d.gid==id)
                    this.edit_detail.goodsw_item.splice(index,1)
                })
            },
            edit_save(type){
                var data_form = {
                    status:type,
                    id:this.edit_detail.id,
                    receipt_name:this.edit_detail.receipt_name,
                    goodsw_item:this.edit_detail.goodsw_item,
                    receipt_no:this.edit_detail.receipt_no
                }
                $.ajax({
                    url:"/admin/gwa.goods_warehousing/edit",
                    type:'POST',
                    data:data_form,
                    dataType: 'json',
                    success:(res)=>{
                        if(res.code==1){
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });
                            this.dialogVisible = false
                            this.get_list()
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.msg
                            });
                        }
                    }
                })
            },
            keydown(e){
                if(e.keyCode === 13){
                    // var i = this.add_form.goodsw_item[this.add_form.goodsw_item.length-1].gid-1+2
                    var reg = /[^\x00-\xff]/g
                    var data = {}
                    if(reg.test(this.order_code_form)){
                        data = {good_name:this.order_code_form}
                    }else{
                        data = {bar_code:this.order_code_form}
                    }
                    $.ajax({
                        url:"/admin/gwa.goods_warehousing/get_tmgood",
                        type:'POST',
                        data:data,
                        dataType: 'json',
                        success:(res)=>{
                            if(res.code==1){
                                this.order_code_form = ''
                                if(res.data.id){
                                    var data_obj = {
                                        // 'goods_warehousing_id':this.add_form.receipt_id,
                                        "gid":res.data.id,
                                        "good_name":res.data.title,
                                        "good_type_id":res.data.good_type_id,
                                        "good_type_name":res.data.good_type_name,
                                        "num":1,
                                        "good_bar_code":res.data.bar_code,
                                    }
                                    if(this.add_form.goodsw_item.findIndex(d=>d.good_bar_code==res.data.bar_code) ==-1){
                                        this.add_form.goodsw_item.push(data_obj)
                                    }
                                }else{
                                    this.$message({
                                        type: 'info',
                                        message: '没有搜索到此商品'
                                    });
                                }

                            }else{
                                this.$message({
                                    type: 'info',
                                    message: res.msg
                                });
                            }
                        }
                    })
                }
            },
            keydown_deit(e){
                if(e.keyCode === 13){
                    var reg = /[^\x00-\xff]/g
                    var data = {}
                    if(reg.test(this.order_code_form)){
                        data = {good_name:this.order_code_form}
                    }else{
                        data = {bar_code:this.order_code_form}
                    }
                    $.ajax({
                        url:"/admin/gwa.goods_warehousing/get_tmgood",
                        type:'POST',
                        data:data,
                        dataType: 'json',
                        success:(res)=>{
                            if(res.code==1 && res.data.id){
                                this.order_code_form = ''

                                var data_obj = {
                                    'id':this.edit_detail.id,
                                    'goods_warehousing_id':this.edit_detail.id,
                                    "gid":res.data.id,
                                    "good_name":res.data.title,
                                    "good_type_id":res.data.good_type_id,
                                    "good_type_name":res.data.good_type_name,
                                    "num":1,
                                    "good_bar_code":res.data.bar_code,
                                }
                                if(this.edit_detail.goodsw_item.findIndex(d=>d.good_bar_code==res.data.bar_code) ==-1){
                                    this.edit_detail.goodsw_item.push(data_obj)
                                }
                                this.edit_detail.goodsw_item.map(item=>{
                                    delete item.id
                                    // item.goods_warehousing_id=this.edit_detail.id
                                    // item.gid=res.data.id
                                    // item.good_name=res.data.title
                                    // item.good_type_id=res.data.good_type_id
                                    // item.good_type_name=res.data.good_type_name
                                    // item.num=1
                                    // item.good_bar_code=res.data.bar_code
                                })
                            }else{
                                this.$message({
                                    type: 'info',
                                    message: res.msg
                                });
                            }
                        }
                    })
                }
            },
            onSave(){
                this.add_form.status = 1
                $.ajax({
                    url:"/admin/gwa.goods_warehousing/add",
                    type:'POST',
                    data:this.add_form,
                    dataType: 'json',
                    success:(res)=>{
                        if(res.code==1){
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });
                            this.dialogVisible = false
                            this.add_form={
                                receipt_name:'',
                                    status:0,
                                    receipt_no:'',
                                    receipt_username:'',
                                    receipt_id:'',
                                    warehousing_time:'',
                                    goodsw_item:[]
                            }
                            this.get_list()

                        }else{
                            this.$message({
                                type: 'info',
                                message: res.msg
                            });
                        }
                    }
                })
            },
            exit(){
                this.dialogVisible = false;
            },
            success_(){
                const loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                this.add_form.status = 2
                $.ajax({
                    url:'/admin/gwa.goods_warehousing/incoming_data',
                    dataType:"json",
                    success:(res)=>{
                        loading.close()
                        this.add_form.receipt_no = res.data.receipt_no
                        this.add_form.receipt_username = res.data.receipt_username
                        this.add_form.receipt_id = res.data.receipt_id
                        this.add_form.warehousing_time = res.data.warehousing_time
                        // this.dialogVisible = true;
                        // this.active_int=3;
                        $.ajax({
                            url:"/admin/gwa.goods_warehousing/add",
                            type:'POST',
                            data:this.add_form,
                            dataType: 'json',
                            success:(res)=>{
                                if(res.code==1){
                                    this.$message({
                                        type: 'success',
                                        message: res.msg
                                    });
                                    this.get_list()
                                    this.dialogVisible = false
                                    this.add_form={
                                        receipt_name:'',
                                        status:0,
                                        receipt_no:'',
                                        receipt_username:'',
                                        receipt_id:'',
                                        warehousing_time:'',
                                        goodsw_item:[]
                                    }


                                }else{
                                    this.$message({
                                        type: 'info',
                                        message: res.msg
                                    });
                                }
                            }
                        })

                    },
                    error:()=>{
                        loading.close();
                        this.$message({
                            type: 'err',
                            message: '网络状态差'
                        });
                    }
                })
            },
            success(){
                $.ajax({
                    url:"/admin/ord.order/change_information",
                    data:{change_info:this.change_info_val,id:this.change_info_id},
                    type:'POST',
                    dataType: 'json',
                    success:(res)=>{
                        if(res.code==1){
                            var item = this.dataArr.find(d=>d.id==this.change_info_id)
                            item.change_info = this.change_info_val
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.msg
                            });
                        }
                    }
                })
                this.dialogVisible_change = false
            },
            change_info(id){
                this.dialogVisible_change = true
                this.change_info_id = id
            },
            cancel_order(id){
                this.$confirm('是否取消此订单？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.ajax({
                        url:"/admin/ord.order/cancel_order",
                        data:{id},
                        type:'POST',
                        dataType: 'json',
                        success:(res)=>{
                            if(res.code==1){
                                var item = this.dataArr.find(d=>d.id==id)
                                item.status = 7
                                this.$message({
                                    type: 'success',
                                    message: res.msg
                                });
                            }else{
                                this.$message({
                                    type: 'info',
                                    message: res.msg
                                });
                            }
                        }
                    })
                }).catch(() => {

                });
            },
            examine(id){
                this.$confirm('修改变更审核状态', '审核', {
                    distinguishCancelAndClose: true,
                    center:true,
                    confirmButtonText: '通过',
                    cancelButtonText: '驳回',
                    type: 'warning',
                    beforeClose:(action, instance, done)=>{
                        if(action=='confirm'){
                            $.ajax({
                                url:"/admin/chao.change_order/change_order_review",
                                data:{id},
                                type:'POST',
                                dataType: 'json',
                                success:(res)=>{
                                    if(res.code==1){
                                        var item = this.dataArr.find(d=>d.id==id)
                                        item.change_status = 2
                                        this.$message({
                                            type: 'success',
                                            message: res.msg
                                        });
                                    }else{
                                        this.$message({
                                            type: 'info',
                                            message: res.msg
                                        });
                                    }
                                    done()
                                }
                            })
                        }else if(action=='cancel'){
                            $.ajax({
                                url:"/admin/chao.change_order/change_order_refuse",
                                data:{id,change_reason:'驳回'},
                                type:'POST',
                                dataType: 'json',
                                success:(res)=>{
                                    if(res.code==1){
                                        var item = this.dataArr.find(d=>d.id==id)
                                        item.change_status = 3
                                        this.$message({
                                            type: 'success',
                                            message: res.msg
                                        });
                                    }else{
                                        this.$message({
                                            type: 'info',
                                            message: res.msg
                                        });
                                    }
                                }
                            })
                            done()
                        }else{
                            done()
                        }
                    }
                })
            },
            onSubmit(){
                $.ajax({
                    url:'/admin/gwa.goods_warehousing/incoming_data',
                    dataType:"json",
                    success:(res)=>{
                        this.add_form.receipt_no = res.data.receipt_no
                        this.add_form.receipt_username = res.data.receipt_username
                        this.add_form.receipt_id = res.data.receipt_id
                        this.add_form.warehousing_time = res.data.warehousing_time
                        this.dialogVisible = true;
                        this.active_int=3;
                        // this.dataArr = res.data
                        // this.total = res.count
                    }
                })
            },

            edit_show(data){
                $.ajax({
                    url:"/admin/gwa.goods_warehousing/goodw_detail",
                    data:{id:data.id},
                    type:'GET',
                    dataType: 'json',
                    success:(res)=>{
                        if(res.code==1){
                            res.row.goodsw_item = res.row.goods_warehousing_item
                            delete res.row.goods_warehousing_item
                            this.edit_detail = res.row
                            this.dialogVisible = true;
                            this.active_int=2;
                        }else{
                            this.$message({
                                type: 'info',
                                message: res.msg
                            });
                        }
                    }
                })
            },
            notes_show(data){
                this.$prompt('请输备注', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    inputPattern: /\S+/,
                    inputErrorMessage: '备注不能为空'
                }).then(({ value }) => {
                    $.ajax({
                        url:'/admin/gwa.goods_warehousing/edit_remark',
                        data:{id:data.id,reamark:value},
                        dataType:"json",
                        success:(res)=>{
                           if(res.code==1){
                               this.$message({
                                   type: 'success',
                                   message:res.msg
                               });
                               var item = this.dataArr.find(d=>d.id==data.id)
                               item.reamark = value
                           }else{
                               this.$message({
                                   type: 'info',
                                   message: res.msg
                               });
                           }
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消输入'
                    });
                });
            },
            createOrder(){
                window.location.href = '/admin/ord.order/add'
            },
            handleSizeChange(val){
                this.data.limit = val
                this.get_list()
            },
            handleCurrentChange(val) {
                this.data.page = val
                this.get_list()
            },
            get_list(){
                $.ajax({
                    url:'/admin/gwa.goods_warehousing/index',
                    data:this.data,
                    dataType:"json",
                    success:(res)=>{
                        this.dataArr = res.data
                        this.total = res.count
                    }
                })
            }
        },
        mounted() {

        }
    })
</script>
<style>
    .search_box .el-input input{
    height: 100px;
}
</style>